package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;
import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHKyHieu;
import com.lb.jwitter.service.TblDKMHKyHieuService;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblTaiKhoanService;
import com.lb.jwitter.service.TblThongTinMonHoc;
import com.lb.jwitter.service.TblThongTinMonHocService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.ExcelToJSon;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblThongTinMonHocSaveFromXLSAction {


	private TblTaiKhoan taiKhoanDangNhap; 
	  
	  public TblTaiKhoan getTaiKhoanDangNhap() { 
	      return taiKhoanDangNhap; 
	  } 
	  public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	      this.taiKhoanDangNhap = taiKhoanDangNhap; 
	  } 
	  
	  private int dotDKId;
	  
	public int getDotDKId() {
		return dotDKId;
	}
	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	private Message msg;
	
	  public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	private TblThongTinMonHocService tblThongTinMonHocService; 
	
	private JSONArray jsons;

	public JSONArray getJsons() {
		return jsons;
	}
	public void setJsons(JSONArray jsons) {
		this.jsons = jsons;
	}
	
	private String destFile;
	public String getDestFile() {
		return destFile;
	}
	public void setDestFile(String destFile) {
		this.destFile = destFile;
	}
	
	public String execute() throws Exception
	{


	     try{ 	
	    	 
	         Map session = ActionContext.getContext().getSession();
	         taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	         if( taiKhoanDangNhap == null) return "noprivilege";
	         
	        ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	        TblCTDTMonHocService tblCTDTMonHocService = (TblCTDTMonHocService)  context.getBean("tbl_CTDT_MonHocClient"); 
	        
	        tblThongTinMonHocService = (TblThongTinMonHocService) context.getBean("tbl_ThongTin_MonHocClient"); 
	        
	        jsons = ExcelToJSon.execute(destFile);
	        
	        //System.out.println(jsons);
	        tblThongTinMonHocService.createListtblThongTinMonHocByJSON(dotDKId, jsons.toString(), taiKhoanDangNhap);
	        
//	        for(int i = 0; i < jsons.size() ; i++){
//	    		JSONObject obj = jsons.getJSONObject(i);
//	    		
//	    		TblThongTinMonHoc tblThongTinMonHoc = new TblThongTinMonHoc();
//
//	    		 //get mon hoc theo ma mon hoc
//	            List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
//	            {
//	            	 ConditionReport con = new ConditionReport(); 
//	                 con.setCol("ma");
//	                 con.setValue1(obj.get("msmh"));
//	                 con.setOperator("eq");
//	                 cons.add(con);	
//	            }
//	            List<TblCTDTMonHoc> monHocDK = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 1, 1);
//
//	            //set mon hoc
//	            if(monHocDK != null){
//	            	tblThongTinMonHoc.setMonHocId(monHocDK.get(0)); 
//	    
//	            	//set dotDK
//		    		TblDKMHDotDK tblDKMHDotDK = new TblDKMHDotDK();
//		    		tblDKMHDotDK.setId(new Long(dotDKId));
//		    		tblThongTinMonHoc.setDotDKId(tblDKMHDotDK);
//		    		
//		           //set Can Bo
//		           tblThongTinMonHoc.setCanBoId(taiKhoanDangNhap.getCanBoId());
//		           
//		           //set ngay cap nhat
//		           DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
//		           Date dd = new Date(); 
//		           GregorianCalendar c = new GregorianCalendar(); 
//		           c.setTime(dd);
//		           XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
//		            tblThongTinMonHoc.setNgayCapNhat(d); 
//		    		
//		    		tblThongTinMonHocService.createtblThongTinMonHoc(tblThongTinMonHoc);
//	            }
//	
//	    	}
	        
	            return Action.SUCCESS; 
	     } 
	     catch(Exception e){ 
	             System.out.println("Loi add ky hieu from excell Action"); 
	             e.printStackTrace(); 
	             return Action.ERROR; 
	     } 

	}
	
	
	
	
	
}
